<?php
namespace Admin\Controller;
use Think\Controller;

class IndexController extends BaseController {
	
    public function index(){
    	$info = session('admin');
    	if($info['role_id'] != 0){
    		$userinfo = M('user')->where("id = {$info['uid']}")->find();
    	}
    	$this->assign('info',$info);
    	$this->assign('userinfo',$userinfo);
        $this->display();
	}
		
	public function main(){
		$admin = session('admin');
		if($admin['role_id'] != 0){
			$map['t_id'] = $admin['id'];
		}
		
		/*今日*/
		$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
		$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
		$beginTodayM=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
		$endTodayM=mktime(0,0,0,date('m'),date('d'),date('Y'));
		$beginTodayMiner = date("Ymd",strtotime($beginTodayM));
		$endTodayMiner = date("Ymd",strtotime($endTodayM));
		
		/*本周*/
		$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
		$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
		$beginLastweekMiner = date("Ymd",strtotime($beginLastweek));
		$endLastweekMiner = date("Ymd",strtotime($endLastweek));		
		/*本月*/
		$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
		$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
		$beginThismonthMiner = date("Ymd",strtotime($beginThismonth));
		$endThismonthMiner = date("Ymd",strtotime($endThismonth));

		$add_today = M('money_record')->where("status = 1 && create_time>=$beginToday && create_time<=$endToday")->sum('amount');
		$del_today = M('money_record')->where("status = 1 && create_time>=$beginToday && create_time<=$endToday")->sum('amount');
		
		$add_week = M('money_record')->where("status = 1 && create_time>=$beginLastweek && create_time<=$endLastweek")->sum('amount');
		$del_week = M('money_record')->where("status = 1 && create_time>=$beginLastweek && create_time<=$endLastweek")->sum('amount');
			
		$add_month = M('money_record')->where("status = 1 && create_time>=$beginThismonth && create_time<=$endLastweek")->sum('amount');
		$del_month = M('money_record')->where("status = 1 && create_time>=$beginThismonth && create_time<=$endLastweek")->sum('amount');
		
		$add_today_miner = M('miner')->where("miner_date>=$beginTodayMIner && miner_date<=$endTodayMiner")->sum('user_ysq_money');
		$del_today_miner = M('miner')->where("miner_date>=$beginTodayMIner && miner_date<=$endTodayMiner")->sum('user_btq_money');	
		
		$add_week_miner = M('miner')->where("miner_date>=$beginLastweekMiner && miner_date<=$endLastweekMiner")->sum('user_ysq_money');
		$del_week_miner = M('miner')->where("miner_date>=$beginLastweekMiner && miner_date<=$endLastweekMiner")->sum('user_btq_money');	
			
		$add_month_miner = M('miner')->where("miner_date>=$beginThismonthMiner && miner_date<=$endThismonthMiner")->sum('user_ysq_money');
		$del_month_miner = M('miner')->where("miner_date>=$beginThismonthMiner && miner_date<=$endThismonthMiner")->sum('user_btq_money');		

		$bbsr = M('money_record')->where("status = 1 ")->sum('amount');
		$bbsc = M('miner')->sum('user_ysq_money');
		$bb = $bbsc / $bbsr;
		$bb = round($bb,2);
		$this->assign('bb',$bb);
		$this->assign('user',M('user')->where($map)->count());
		$this->assign('add_today',$add_today);
		$this->assign('add_week',$add_week);
		$this->assign('add_month',$add_month);
		$this->assign('del_today',$del_today);
		$this->assign('del_week',$del_week);
		$this->assign('del_month',$del_month);
		$this->assign('add_today_miner',$add_today_miner);
		$this->assign('del_today_miner',$del_today_miner);
		$this->assign('add_week_miner',$add_week_miner);
		$this->assign('del_week_miner',$del_week_miner);
		$this->assign('add_month_miner',$add_month_miner);
		$this->assign('del_month_miner',$del_month_miner);
		$this->assign('today',M('user')->where($map)->where("reg_time >= $beginToday && reg_time<=$endToday")->count());
		$this->assign('week',M('user')->where($map)->where("reg_time >= $beginLastweek && reg_time<=$endLastweek")->count());
		$this->assign('month',M('user')->where($map)->where("reg_time >= $beginThismonth && reg_time<=$endThismonth")->count());
		$this->display();
	}
	
	public function pwd() {
		$user2 = session('admin');
		if($user2['role_id'] != 0){
			$User = M('agent');
		}else{
			$User = M('admin');
		}
		if ($_POST) {
			
			if (!IS_AJAX) {
				$this->error('提交方式不正确', U('index/pwd'), 0);
			} else {
				$data['username'] = I('post.user');
				$data['password'] = md5(I('post.oldpassword'));
				$newpassword = md5(I('post.newpassword'));
				$repassword = md5(I('post.repassword'));
				
				$result = $User->where($data)->find();

				if ($result) {
					if ($newpassword != $repassword) {
						$this->error("两次输入新密码不一致");
					} else {
						$User->where($data)->setField('password', $newpassword);
						$this->success("修改成功", U('Login/index'),1);
					}
				} else {
					$this->error("账号或密码不正确");
				}
			}
		}
		$this -> assign('user2', $user2);
		$this -> display();
	}

	public function del() {
		delFileByDir(APP_PATH.'Runtime/');
		$this->success('删除缓存成功！',U('Admin/Index/index'));
	}	
	
	
		
}